using System;
using System.Collections.Generic;
using System.Text;
using Emlyn.Common.Tools.BusinessLayer;

namespace Emlyn.Website.Articles
{
    public class Article: BaseItem
    {
        public Article()
            : base(true)
        {
            _articleID = Guid.NewGuid();
            Initialise();
        }

        public Article(Guid aArticleID)
            : base(false)
        {
            _articleID = aArticleID;
            Initialise();
        }

        private void Initialise()
        {
            _articleCode = GenerateCode();
        }

        private Guid _articleID;
        public Guid ArticleID
        {
            get { return _articleID; }
        }

        private string _articleCode;

        public string ArticleCode
        {
            get { return _articleCode; }
            set { _articleCode = value; }
        }

        private string _articleName;

        public string ArticleName
        {
            get { return _articleName; }
            set { _articleName = value; }
        }

        public string ArticleDisplayName
        {
            get 
            {
                return _articleName + " (" + _lastChanged.ToString() + ")";
            }
        }

        private string _url;

        public string Url
        {
            get 
            { 
                if (String.IsNullOrEmpty(_url))
                    return String.Empty;
                else
                    return _url; 
            }
            set { _url = value; }
        }

        private string _articleContent;

        public string ArticleContent
        {
            get { return _articleContent; }
            set { _articleContent = value; }
        }

        private DateTime _lastChanged;

	    public DateTime LastChanged
	    {
		    get { return _lastChanged;}
		    set { _lastChanged = value;}
	    }

        private static string GenerateCode()
        {
            string lcode = ((new Random()).Next(99999999)).ToString();
            ArticleQuery laq = new ArticleQuery();

            while (laq.GetArticleByArticleCode(lcode) != null)
            {
                lcode = ((new Random()).Next(99999999)).ToString();
            };

            return lcode;
        }
    }
}
